package com.mycjj.android.activity;

import java.util.Map;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.Adviser;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

//留言评论界面
public class Point_CommentActivity extends BaseActivity implements android.view.View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_point_comment);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("在线咨询");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Point_CommentActivity.this.finish();
			}
		});
	}

	private TextView mTv_AdviserName;
	private ImageView mIv_Tel;
	private Button mBtn_setDefaultAdviser;
	private Button mBtn_Send;
	private EditText mEt_message;

	Adviser adviser;
	private int type;
	private String carId;
	private void init() {
		Intent intent = getIntent();
		if (intent != null) {
			type = intent.getIntExtra("type", 0);
		}
		mTv_AdviserName = (TextView) findViewById(R.id.tv_adviser_name);
		mIv_Tel = (ImageView) findViewById(R.id.iv_tel);
		mBtn_setDefaultAdviser = (Button) findViewById(R.id.btn_set_default_adviser);
		mBtn_Send = (Button) findViewById(R.id.btn_send);
		mEt_message = (EditText) findViewById(R.id.et_message);

		mBtn_Send.setOnClickListener(this);
		mBtn_setDefaultAdviser.setOnClickListener(this);
		mIv_Tel.setOnClickListener(this);
		carId = Preferences.getSharedPreferences(mApp, "carId", "");
		if (type == 0) {// 列表数据过来
			adviser = (Adviser) intent.getSerializableExtra("data");
			mBtn_setDefaultAdviser.setVisibility(View.VISIBLE);
		} else {// 点击直接过来
			adviser = dataManager.getCarInfoBaseBean().getData().getAdviser();
			mBtn_setDefaultAdviser.setVisibility(View.INVISIBLE);
		}
	}

	private void initData() {
		if (adviser != null) {
			mTv_AdviserName.setText("顾问:" + adviser.getName());
		}
	}

	private void tel() {
		if (adviser == null || TextUtils.isEmpty(adviser.getPhone())) {
			Log.e("mark", "adviser is null or phone is null!!!!");
			return;
		}
		Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + adviser.getPhone()));
		startActivity(intent);
	}

	private void set() {
		if (adviser == null) {
			Log.e("mark", "adviser is null!");
			return;
		}
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("car_id", carId);
		params.put("adviser_id", adviser.getId());
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestStoreBindAdviserWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>) dataManager.getBaseBean();
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						Toast.makeText(Point_CommentActivity.this, "绑定顾问成功!", Toast.LENGTH_SHORT).show();
						try{
							dataManager.getCarInfoBaseBean().getData().setAdviser(adviser);
							dataManager.setStoreChange(false);
						}catch(Exception e){
							e.printStackTrace();
						}
						mBtn_setDefaultAdviser.setVisibility(View.INVISIBLE);
					} else {
						Toast.makeText(Point_CommentActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					Log.e("mark", "BaseBean is null!");
				}
			}
		});

	}

	private void send() {
		if (adviser == null) {
			Log.e("mark", "adviser is null!");
			return;
		}
		String message = mEt_message.getText().toString().trim();
		if(TextUtils.isEmpty(message)){
			Toast.makeText(Point_CommentActivity.this, "留言内容不能为空!!", Toast.LENGTH_SHORT).show();
			return;
		}
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("store_id", adviser.getStore_id());
		params.put("adviser_id", adviser.getId());
		params.put("message", message);
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestStoreAddMessageWithDoneHandler("",params,new AsyncHttpResponseHandler(){
			
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>) dataManager.getBaseBean();
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						Toast.makeText(Point_CommentActivity.this, "发送留言成功!", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(Point_CommentActivity.this, "code:" + baseBean.getCode() + ";message:" + baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					Log.e("mark", "BaseBean is null!");
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_tel:
			tel();
			break;
		case R.id.btn_set_default_adviser:
			set();
			break;
		case R.id.btn_send:
			send();
			break;
		}
	}

}
